import Vue from 'vue';
// 导入 vuex
import Vuex from 'vuex';
// 使用vuex
Vue.use(Vuex);

const carinfo = JSON.parse(localStorage.getItem('car') || '[]');
export default new Vuex.Store({
  state: { // this.$store.state.***
    car: carinfo,
  },
  mutations: { // this.$store.commit('方法名称'，'唯一参数')
    addToCar(state, goodsobj) { // 增加商品到购物车
      let flag = false;
      state.car.some((item) => {
        if (item.id === goodsobj.id) {
          // eslint-disable-next-line no-param-reassign
          item.count +=
          // eslint-disable-next-line radix
          parseInt(goodsobj.count);
          flag = true;
          return true;
        }
        return false;
      });
      if (!flag) {
        state.car.push(goodsobj);
      }
      // 更新car成功后，将其存到localstorage
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updateGoodsInfo(state, goodsinfo) { // 更新购物车中，商品的数量
      state.car.some((item) => {
        // eslint-disable-next-line eqeqeq
        if (item.id == goodsinfo.id) {
          // eslint-disable-next-line no-param-reassign
          item.count =
          // eslint-disable-next-line radix
          parseInt(goodsinfo.count);
          return true;
        }
        return false;
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    removeGoodsItem(state, id) { // 删除购物车中的商品
      state.car.some((item, i) => {
        // eslint-disable-next-line eqeqeq
        if (item.id == id) {
          state.car.splice(i, 1);
          return true;
        }
        return false;
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updateGoodsSelected(state, goodsinfo) { // 更新购物车，商品选中状态
      state.car.some((item) => {
        // eslint-disable-next-line eqeqeq
        if (item.id == goodsinfo.id) {
          // eslint-disable-next-line no-param-reassign
          item.selected = goodsinfo.selected;
          return true;
        }
        return false;
      });
      localStorage.setItem('car', JSON.stringify(state.car));
    },
  },
  getters: { // this.$store.getters.***
    getAllCount(state) { // 获取所有商品总件数
      let sum = 0;
      state.car.forEach((item) => {
        sum += item.count;
      });
      return sum;
    },
    getGoodsCount(state) { // 获得每件商品对应的数量
      // eslint-disable-next-line prefer-const
      let obj = {};
      state.car.forEach((item) => {
        obj[item.id] = item.count;
      });
      return obj;
    },
    getGoodsSelected(state) { // 获得每件商品是否选中
      // eslint-disable-next-line prefer-const
      let obj = {};
      state.car.forEach((item) => {
        obj[item.id] = item.selected;
      });
      return obj;
    },
    getGoodsCountAndAmount(state) { // 获得购物车中，勾选的件数，和 总价格
      // eslint-disable-next-line prefer-const
      let obj = { count: 0, amount: 0 };
      state.car.forEach((item) => {
        if (item.selected) {
          obj.count += item.count;
          obj.amount += item.price * item.count;
        }
      });
      return obj;
    },
  },
});
